package edu.ruc.cluster.util;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class TestUser {
	
	//SSH_MSG_DISCONNECT: 2 Too many authentication failures for txt
	private DefaultLogHandler logHandler = null;

	private JSch jsch = null;
	private Session session = null;
	private UserInfo userinfo = null;
	private String host = null;
	private String passwd = null;
	
	public TestUser(String host, String passwd){
		
		jsch = new JSch();
		userinfo = new MyUserInfo(passwd);		
		logHandler = DefaultLogHandler.getInstance();
		
		this.host = host;
		this.passwd = passwd;
	}
	public boolean isConnect(){

		String user = host.substring(0, host.indexOf('@'));
		host = host.substring(host.indexOf('@') + 1);
		
		try{
			System.out.println("user : " + user + " host: " + host);
			session = jsch.getSession(user, host, 22);
			session.setPassword(passwd);
			session.setUserInfo(userinfo);
			session.connect(2000);
			
		}catch(JSchException e){
			logHandler.error(this.getClass().getName() + e.getMessage());
			return false;
		}finally{
			session.disconnect();
		}
		return true;
	}
	
	
	public static void main(String[] args){
		TestUser t1 = new TestUser("deke@10.77.110.229", "111111");
		System.out.println(t1.isConnect());
//		TestUser t2 = new TestUser("abc@dlnode27", "111111");
//		System.out.println(t2.isConnect());
//		TestUser t3 = new TestUser("deke@dlnode27", "123456");
//		System.out.println(t3.isConnect());
	}
	
}
